//
//  StockBrand.m
//  MarketDataAnalyzer
//
//  Created by jiro on 10/01/23.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "StockBrandJa.h"
#import "../DbConstants.h"
#import "DbUtility.h"
#import "MarketInitialJaType.h"

@implementation StockBrandJa

@synthesize marketInitial;
@synthesize existFlag;
@synthesize lastDailyExchangeDataCollectDate;

+ (NSString*) tableName
{
	return kText_StockBrandJaTable;
}

+ (NSString*) queryCreateTable
{ 
	NSString* s = [ NSString stringWithFormat: @"create table %@ ( %@ %@, %@ %@, %@ %@( %d ), %@ %@, %@ %@, %@( %@, %@ ) );",
		[ StockBrandJa tableName ],
		kText_Code, kText_Text,
		kText_Name, kText_Text,
		kText_MarketInitial, kText_VarChar, 4,
		kText_ExistFlag, kText_Boolean, 
		kText_LastExchangeDataCollected, kText_TimestampWithTimeZone,
		kText_PrimaryKey, kText_Code, kText_MarketInitial ];
	 return s;
	
}

+ (id) stockBrandJaWithDbResponse: (NSArray*) values
{
	StockBrandJa* ret = [ [ StockBrandJa alloc ] init ];

	ret.code = [ values objectAtIndex: 0 ];
	ret.name = [ values objectAtIndex: 1 ];
	ret.marketInitial = [ values objectAtIndex: 2 ];
	ret.existFlag = [ [ values objectAtIndex: 3 ] isEqualToString: @"t" ];
	ret.lastDailyExchangeDataCollectDate =
		[ NSDate dateWithString: [ DbUtility convertIsoStdDateStringToCocoaDateString: [ values objectAtIndex: 4 ] ] ];

	return ret;
}

+ (NSString*) queryInsertInitialValues
{
	NSMutableString* s = [ NSMutableString string ];
//	for ( int i = 1000; i < 1005; i++ )
	for ( int i = 1000; i < 9999; i++ )
	{
		StockBrandJa* brand = [ [ StockBrandJa alloc ] init ];
		brand.code = [ NSString stringWithFormat: @"%d", i ];
		brand.name = @"";
		brand.existFlag = NO;
		brand.lastDailyExchangeDataCollectDate = [ NSDate dateWithString: @"1980-1-1 00:00:00 +0900" ];
		
		brand.marketInitial = MarketInitialJaType::ToString( MarketInitialJaType::Tokyo );
		if ( i != 1000 )
		{
			[ s appendString: @", " ];
		}
		[ s appendString: [ brand queryInsertValue ] ];

		brand.marketInitial = MarketInitialJaType::ToString( MarketInitialJaType::Osaka );
		[ s appendString: @", " ];
		[ s appendString: [ brand queryInsertValue ] ];

		brand.marketInitial = MarketInitialJaType::ToString( MarketInitialJaType::Nagoya );
		[ s appendString: @", " ];
		[ s appendString: [ brand queryInsertValue ] ];

	}
	return s;
}

+ (NSString*) querySelectAll
{
	NSString* s = [ NSString stringWithFormat: @"select * from %@;", [ StockBrandJa tableName ] ];
	return s;
}

- (NSString*) queryInsert
{
	NSString* s = [ NSString stringWithFormat: @"insert into * values %@;", 
		[ StockBrandJa tableName ], [ self queryInsertValue ] ];
	return s;
}

- (NSString*) queryInsertValue
{
	NSString* s = [ NSString stringWithFormat: @"( '%@', '%@', '%@', %@, '%@' )",
		code, name, marketInitial, existFlag ? @"true" : @"false", lastDailyExchangeDataCollectDate
	];
	return s;
}

@end
